package com.itfreer.utils.hibernate;

import org.hibernate.Criteria;
import org.hibernate.criterion.ProjectionList;
import org.hibernate.criterion.Projections;
import org.hibernate.transform.Transformers;

/**
 * 定义查询字段转换器
 */
public class QueryFieldsConvertUtils {
	
	/**
	 * 查询字段转换
	 * @param crit
	 * @param order
	 */
	public static void convert(
			Criteria crit,
			String queryFields, Class<?> pc){
		
		if(queryFields==null || queryFields.trim().equals("")){
			return;
		}
		
		// 查询字段
		ProjectionList pList = Projections.projectionList(); 
		String[] fiels = queryFields.split(",");
		for(String field : fiels){
			pList.add(Projections.property(field).as(field));  
		}
		crit.setProjection(pList).setResultTransformer(Transformers.aliasToBean(pc));
	}
}
